#include "sdk.h"
#include "configuretoolsdlg.h"
#include "edittooldlg.h"

BEGIN_EVENT_TABLE( ConfigureToolsDlg, wxScrollingDialog )
  EVT_BUTTON( XRCID( "btnAdd" ), ConfigureToolsDlg::OnAdd )
  EVT_BUTTON( XRCID( "btnEdit" ), ConfigureToolsDlg::OnEdit )
  EVT_BUTTON( XRCID( "btnRemove" ), ConfigureToolsDlg::OnRemove )
  EVT_BUTTON( XRCID( "btnAddSeparator" ), ConfigureToolsDlg::OnAddSeparator )
  EVT_BUTTON( XRCID( "btnUp" ), ConfigureToolsDlg::OnUp )
  EVT_BUTTON( XRCID( "btnDown" ), ConfigureToolsDlg::OnDown )
  EVT_UPDATE_UI( -1, ConfigureToolsDlg::OnUpdateUI )
END_EVENT_TABLE()

ConfigureToolsDlg::ConfigureToolsDlg( wxWindow* parent ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置工具.xrc" );
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "dlgConfigureTools" ), _T( "wxScrollingDialog" ) ) ) {
    return;
  }
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  DoFillList();
}

ConfigureToolsDlg::~ConfigureToolsDlg() {
}

void ConfigureToolsDlg::DoFillList() {
  wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  list->Clear();
  ToolsManager* toolMan = Manager::Get()->GetToolsManager();
  int count = toolMan->GetToolsCount();
  for( int i = 0; i < count; ++i ) {
    if( const cbTool* tool = toolMan->GetToolByIndex( i ) ) {
      list->Append( tool->GetName() );
    }
  }
}

bool ConfigureToolsDlg::DoEditTool( cbTool* tool ) {
  if( !tool ) {
    return false;
  }
  EditToolDlg dlg( this, tool );
  PlaceWindow( &dlg );
  return dlg.ShowModal() == wxID_OK;
}


void ConfigureToolsDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  const wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  bool hasSel = list->GetSelection() != -1;
  bool notFirst = list->GetSelection() > 0;
  bool notLast = ( list->GetSelection() < ( int )( list->GetCount() ) - 1 ) && hasSel;
  bool notSeparator = true;
  if( hasSel ) {
    notSeparator = Manager::Get()->GetToolsManager()->GetToolByIndex( list->GetSelection() )->GetName() != CB_TOOLS_SEPARATOR;
  }
  XRCCTRL( *this, "btnEdit",   wxButton )->Enable( hasSel && notSeparator );
  XRCCTRL( *this, "btnRemove", wxButton )->Enable( hasSel );
  XRCCTRL( *this, "btnUp",     wxButton )->Enable( notFirst );
  XRCCTRL( *this, "btnDown",   wxButton )->Enable( notLast );
}

void ConfigureToolsDlg::OnAdd( M_Code_unused wxCommandEvent& event ) {
  cbTool tool;
  if( DoEditTool( &tool ) ) {
    Manager::Get()->GetToolsManager()->AddTool( &tool );
    DoFillList();
  }
}

void ConfigureToolsDlg::OnEdit( M_Code_unused wxCommandEvent& event ) {
  const wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  cbTool* tool = Manager::Get()->GetToolsManager()->GetToolByIndex( list->GetSelection() );
  DoEditTool( tool );
  DoFillList();
}

void ConfigureToolsDlg::OnRemove( M_Code_unused wxCommandEvent& event ) {
  const wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  int sel = list->GetSelection();
  if( Manager::Get()->GetToolsManager()->GetToolByIndex( sel )->GetName() == CB_TOOLS_SEPARATOR
      || msging( _T( "Are you sure you want to remove this tool?" ),
                 _T( "Remove tool?" ),
                 wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION, this ) == wxID_YES ) {
    Manager::Get()->GetToolsManager()->RemoveToolByIndex( sel );
    DoFillList();
  }
}

void ConfigureToolsDlg::OnAddSeparator( M_Code_unused wxCommandEvent& event ) {
  cbTool tool;
  tool.SetName( CB_TOOLS_SEPARATOR );
  tool.SetCommand( CB_TOOLS_SEPARATOR );
  Manager::Get()->GetToolsManager()->AddTool( &tool );
  DoFillList();
}

void ConfigureToolsDlg::OnUp( M_Code_unused wxCommandEvent& event ) {
  wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  int sel = list->GetSelection();
  cbTool tool( *( Manager::Get()->GetToolsManager()->GetToolByIndex( sel ) ) );
  Manager::Get()->GetToolsManager()->RemoveToolByIndex( sel );
  Manager::Get()->GetToolsManager()->InsertTool( sel - 1, &tool );
  DoFillList();
  list->SetSelection( sel - 1 );
}

void ConfigureToolsDlg::OnDown( M_Code_unused wxCommandEvent& event ) {
  wxListBox* list = XRCCTRL( *this, "lstTools", wxListBox );
  int sel = list->GetSelection();
  cbTool tool( *( Manager::Get()->GetToolsManager()->GetToolByIndex( sel ) ) );
  Manager::Get()->GetToolsManager()->RemoveToolByIndex( sel );
  Manager::Get()->GetToolsManager()->InsertTool( sel + 1, &tool );
  DoFillList();
  list->SetSelection( sel + 1 );
}// end of OnDown
